package com.dsg.controller.apply;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyDda;
import com.dsg.service.apply.IDsgApplyDdaService;
import com.dsg.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * DDA設立記錄 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-10-06
 */
@RestController
@RequestMapping("/apply/dsgApplyDda")
public class DsgApplyDdaController extends BaseController {

    @Autowired
    private IDsgApplyDdaService dsgApplyDdaService;

    /**
     * 查询列表（分页）
     *
     * @param dsgApplyDda
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgApplyDda dsgApplyDda) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("cusNo").setAsc(false));
            page.setOrders(orderItemList);
        }
        dsgApplyDda.setEntCode(SingletonLoginUtils.getSystemEntCode());
//        dsgApplyDda.setNodeName(WorkflowService3.NODE_DDA);
        page = dsgApplyDdaService.selectByCusDdaList(page, dsgApplyDda);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 導入DDA結果
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importDDA")
    @ApiOperation("導入DDA結果")
    public AjaxResult importDda(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        List<String> list = FileUtil.convertStreamToList(file);
        if (CollectionUtil.isNotEmpty(list)) {
            dsgApplyDdaService.importDda(list);
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return json(MessageConstants.SSO_STATUS_FAIL, "導入文件內容為空！");
        }
    }

    /**
     * 導入DDA Change結果
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importDdaChange")
    @ApiOperation("導入DDA Change結果")
    public AjaxResult importDdaChange(@ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        List<String> list = FileUtil.convertStreamToList(file);
        if (CollectionUtil.isNotEmpty(list)) {
            dsgApplyDdaService.importDdaChange(list);
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return json(MessageConstants.SSO_STATUS_FAIL, "導入文件內容為空！");
        }
    }


}
